//
//  101. 对称二叉树.swift
//  Top600
//
//  Created by rjb on 2021/7/29.
//

import Foundation

class Solution101 {
    // 以root节点的左子树与右子树是否对称
    func isSymmetric(_ root: TreeNode?) -> Bool {
        var result: Bool = true
        result = isSubTreeSymmetric(root?.left, right: root?.right)
        return result
    }
    // 两个字树是否相等
    func isSubTreeSymmetric(_ left: TreeNode?, right: TreeNode?) -> Bool {
        if left == nil && right == nil {
            return true
        }
        if left == nil && right != nil {
            return false
        }
        if left != nil && right == nil {
            return false
        }
        if left!.val != right!.val {
            return false
        }
        let flag1 = isSubTreeSymmetric(left?.left, right: right?.right)
        let flag2 = isSubTreeSymmetric(left?.right, right: right?.left)
        return flag1 && flag2
    }
    static func test() {
        let solution = Solution101()
        
        let node1 = TreeNode(1)
        let node2 = TreeNode(2)
        let node22 = TreeNode(2)
        let node3 = TreeNode(3)
        let node4 = TreeNode(4)
        let node44 = TreeNode(4)
        let node33 = TreeNode(3)
        
        node1.left = node2
        node1.right = node22
        node2.left = node3
        node2.right = node4
        node22.left = node44
        node22.right = node33
        
        let result = solution.isSymmetric(node1)
        print(result)
    }
}
